package com.hdl.controller;

import com.hdl.constants.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 类名 LoginController
 * 描述 TODO
 *
 * @author hedonglin
 * @version 1.0
 * @date 2020/5/16 10:15
 */
@Controller
public class LoginController {
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model){
        model.addAttribute("date", new Date());
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String doLogin(String username,String password, HttpSession session, RedirectAttributes redirectAttributes){
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            redirectAttributes.addFlashAttribute("errMsg", "用户名密码均不能为空");
            return "redirect:/login";
        }
        if (Constants.DEFAULT_USERNAME.equals(username)
                && Constants.DEFAULT_PASSWORD.equals(password)){
            // 设置Session
            session.setAttribute(Constants.USER_SESSION_KEY, username);
            return "redirect:/resume/list";
        }else {
            redirectAttributes.addFlashAttribute("errMsg", "用户名/密码错误");
            return "redirect:login";
        }
    }
}
